using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Library_management_system
{
    public partial class ReaderQueryForm : Form
    {
        public ReaderQueryForm()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (!int.TryParse(txtReaderId.Text.Trim(), out int readerId))
            {
                MessageBox.Show("请输入有效的读者号！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            using (SqlConnection conn = DatabaseConfig.GetConnection())
            {
                conn.Open();
                string sql = @"SELECT b.Title AS BookTitle, br.BorrowDate, br.DueDate, br.ReturnDate 
                             FROM BorrowRecords br
                             JOIN Books b ON br.BookID = b.BookID
                             WHERE br.ReaderID = @ReaderID";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@ReaderID", readerId);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("未找到该读者的借阅记录。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dgvBorrows.DataSource = dt;
            }
        }
    }
}